package com.lanou.bookstore.book.dao;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDao {

    public List<Book> findAll() {
        List<Book> books = new ArrayList<>();
        String findAll = "SELECT * FROM book";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement statement = conn.prepareStatement(findAll);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setBname(rs.getString("bname"))
                        .setPrice(rs.getFloat("price"))
                        .setAuthor(rs.getString("author"))
                        .setImage(rs.getString("image"))
                        .setCid(rs.getString("cid"))
                        .setBid(rs.getString("bid"));
                books.add(book);
            }
            rs.close();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return books;
    }

    public List<Book> findBycategory(int cid) {
        List<Book> books = new ArrayList<>();
        String find = "SELECT * FROM book WHERE cid = ?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement statement = conn.prepareStatement(find);
            statement.setInt(1, cid);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setBname(rs.getString("bname"))
                        .setPrice(rs.getFloat("price"))
                        .setAuthor(rs.getString("author"))
                        .setImage(rs.getString("image"))
                        .setCid(rs.getString("cid"))
                        .setBid(rs.getString("bid"));
                books.add(book);
            }
            rs.close();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return books;
    }

    public Book load(String bid) {

        String load = "SELECT * FROM book WHERE bid = ?";
        Connection conn = JDBCUtil.getConn();
        Book book = new Book();
        try {
            PreparedStatement statement = conn.prepareStatement(load);
            statement.setString(1, bid);
            ResultSet rs = statement.executeQuery();
            rs.next();
            book.setBname(rs.getString("bname"))
                    .setPrice(rs.getFloat("price"))
                    .setAuthor(rs.getString("author"))
                    .setImage(rs.getString("image"))
                    .setCid(rs.getString("cid"))
                    .setBid(rs.getString("bid"));
            rs.close();
            statement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return book;
    }
}
